package com.zwz.service.impl;

import com.zwz.dto.BankruptcyDTO;
import com.zwz.entity.BankruptcyReorganization;
import com.zwz.entity.EnterpriseInfo;
import com.zwz.entity.RiskInfoMain;
import com.zwz.mapper.BankruptcyReorganizationMapper;
import com.zwz.mapper.RiskInfoMainMapper;
import com.zwz.service.BankruptcyReorganizationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class BankruptcyReorganizationServiceImpl implements BankruptcyReorganizationService {

    @Autowired
    private RiskInfoMainMapper mainMapper;

    @Autowired
    private BankruptcyReorganizationMapper bankruptcyMapper;

    @Override
    public BankruptcyDTO createWithMain( String enterpriseId ) {
        // 主表操作
        EnterpriseInfo enterpriseInfo=new EnterpriseInfo();
        enterpriseInfo.setId(Long.valueOf(enterpriseId));

        // 子表操作
        BankruptcyReorganization entity = new BankruptcyReorganization();
        int k=0;
        entity.setId(k++);
        entity.setHasBankruptcy(false);
        entity.setParticipantRole("123");

        validateEntity(entity);
        entity.setEnterpriseId(Math.toIntExact(enterpriseInfo.getId()));
        bankruptcyMapper.insert(entity);

        return new BankruptcyDTO(enterpriseInfo, entity);
    }

    private void validateEntity(BankruptcyReorganization entity) {
        if (Boolean.TRUE.equals(entity.getHasBankruptcy())) {
            if (StringUtils.isBlank(entity.getHandlingCourt())) {
                throw new IllegalArgumentException("破产案件必须注明办理法院");
            }
            if (StringUtils.isBlank(entity.getParticipantRole())) {
                throw new IllegalArgumentException("必须指定参与身份类型");
            }
        }
    }
}
